<?php

/**
 * Description of cfBootstrapMenu
 *
 * @author Mariano
 */
class mcBootstrapMenu {

    private $_header = '';
    private $_content = '';
    private $_footer = '';
    private $_user = null;

    public function __construct($user) {
        $this->_user = $user;
        $header = '
            <div id="main-menu" class="navbar">
                <div class="navbar-inner">
                    <div class="container">
                        <div class="nav-collapse collapse navbar-responsive-collapse">
                            <ul class="nav pull-left">';
        $footer = '
                            </ul>
                        </div>
                    </div>
                </div>
            </div>';
        $content = '';

        $items = sfConfig::get('app_mc_menu_items');
        foreach ($items as $a => $item) {
            if (self::hasPermission($item, $this->_user)) {
                $content .=
                        '<li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">' . $a . ' <b class="caret"></b></a>
                                <div class="dropdown-menu">';
                foreach ($item['columns'] as $b => $column) {
                    if (self::hasPermission($column, $this->_user)) {
                        if($b == 'empty') {
                            $column_header = '';
                        } else {
                            $column_header = '<li class="column-header">' . $b . '</li>';
                        }
                        $content .= '<ul class="column">'.$column_header;
                        foreach ($column['items'] as $c => $subitem) {
                            if (self::hasPermission($subitem, $this->_user)) {
                                $content .= '<li><a href="' . url_for('@' . $subitem['url']) . '"><i class="icon-' . $subitem['image'] . '"></i> ' . $c . '</a></li>';
                            }
                        }
                        $content .= '</ul>';
                    }
                }
                $content .=
                        '</div></li>';
            }
        }
        
        if(sfConfig::get('app_mc_menu_logout_route') && $user->isAuthenticated()) {
            $content .= '
                </ul>
                <ul class="nav pull-right">
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown"><i class="icon-user"></i> <b class="caret"></b></a>
                        <div class="dropdown-menu" style="width: 250px;">
                            <ul class="column">
                                <li class="column-header">'.$user.'</li>
                                <li><a href="'.  url_for('@'.sfConfig::get('app_mc_menu_logout_route')).'"><i class="icon-off"></i> Salir</a></li>
                            </ul>
                        </div>
                    </li>
                </ul>
            ';
        }
        
        $this->_header = $header;
        $this->_content = $content;
        $this->_footer = $footer;

        return $this;
    }

    public function render() {
        return $this->_header . $this->_content . $this->_footer;
    }

    /**
     * Check if the user the necessary credentials to see this particular item
     * 
     * @param array $item
     * @param sfUser $user
     */
    public static function hasPermission($item, $user) {
        if (!$user->isAuthenticated()) {
            return false;
        }

        return isset($item['credentials']) ? $user->hasCredential($item['credentials']) : true;
    }

    /**
     * Check if the supplied route exists
     * 
     * @param string $route
     * @param sfContext $context
     * 
     * @return boolean
     */
    public static function routeExists($route, sfContext $context) {
        try {
            $context->getRouting()->generate($route);
            return true;
        } catch (Exception $e) {
            return false;
        }
    }

}

?>
